How will SpaceText be tested?
For SpaceText to be tested, however, it clearly must be contained in a complete program. We will have to write it, as well as SpaceText itself. The test program is not part of the solution to the problem, but it is necessary to show that the function works correctly.
We've done the same kind of activity in earlier case studies. There, we applied the strategy of incremental development to test functions and functions a few at a time. This typically involved writing a driver program to read values, send them to the function, and then print out the result it returned. The difference is that we knew in earlier case studies what the remainder of the program looked like; here, we're not allowed to make any assumptions about the calling program.
Why would someone design a function without a program to accompany it?
This kind of programming happens often in team projects. Someone decides how to split up the solution to a problem; members of the team then design and develop pieces of the solution and put them all together afterward. If the pieces are sufficiently well specified, then the program should work perfectly.
A programmer may also wish to create a library of subprograms to use in future solutions. The contents of the library could be subprograms that perform common actions or computations.
Stop & Help
How could SpaceText be used in a program to align the left and right margins of text in paragraphs?
Why should the function be independent of the calling program?
In either situation, the advantages of designing the function to minimize its dependence on the calling program are clear. When programming in a team, one should make the tasks of the other team members as easy as possible. The more their code has to set up when calling a function, the
more chances there are for errors. When preparing a function for a library, one should make it general, that is, usable in a variety of programs. The more dependent a subprogram is on the calling program, the less general it will be, and the fewer opportunities one will have to use it without modifying it.
It's good to make subprograms as independent of their caller as possible, even in code we write for ourselves, not intended for a library. Such modular code, able to be plugged into a program like an amplifier or tape deck module into a stereo system, will be useful in future programs. Moreover,it's more understandable; one can always look to the subprogram parameters to see what effect the subprogram has on the calling program, rather than having to look through the entire program listing for uses of global variables.
What will the program to test SpaceText do?
Back to the design of SpaceText. It won't be difficult to create a program to test SpaceText. In addition, if SpaceText needs incremental development, it would be nice to have a program for testing the increments already created. We can design test data for SpaceText using the black-box approach, without even knowing what the code is. Finally, inventing a test program will give us a sense of accomplishment about finishing part of the problem. Thus, we decide to start by designing a test program and test data.
The test program should read values for SpaceText's arguments, call SpaceText, then print the results as in earlier case studies. Including "input, process, output" in a loop will make it easier to test SpaceText on a variety of data.
Stop & Help
Describe the template needed to create an "input, process, output" loop.
The calling program needs to supply four things to SpaceText: a line, a length, a desired length, and a status variable. The line is just a character array; its contents and length can be read using the ReadLine function from the Is It Legal? programs. The desired length is an integer no greater than the maximum number of characters in the line; the ReadlnInteger function from Is It Legal? or a simpler function ReadDesired can be used to get this value. Here is the code:
while True:
ReadLine(line, length)
ReadDesired (desiredLength)
SpaceText(line, length, desiredLength, status)
print line, length, and status
How are the variable types defined?
All the variables must of course be declared. The problem statement says that line is of type StringType, an array of characters. For the purposes of testing, we can choose the size for the array. We code this as a Python constant, and declare the array as follows:
MAXSTRLEN = 30 # large enough for testing
line = "" # Array of characters
Similar declarations appeared in the programs for Roman Calculator Construction and Is It Legal?
The length and desiredLength variables will be declared as integers. But what about status? We aren't told much about it: only that it is of type StatusType, as are the constants ENDBLANK, TOOLONG, EMPTYLINE, ONEWORD,and OK. StatusType can be defined in several ways. Recall from The Calendar Shop and Is It Legal? that similar constants were defined as integers. We decide to do the same thing for the purposes of testing.